package com.coding.controller;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.pojo.Group;
import com.coding.service.GroupService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @auther wangchenghao
 * @date 2020/7/31
 **/
@Controller
@ResponseBody
@RequestMapping("group")
public class GroupController {
    @Autowired
    GroupService groupService;

    @ApiOperation(value = "搜索圈子信息（附带已加入信息)")
    @RequestMapping(value = "/searchGroup", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel searchGroup(@ApiParam(value = "user_id", required = true) @RequestParam("user_id") int user_id,
                                 @ApiParam(value = "搜索圈子内容", required = true) @RequestParam("text") String text,
                                 @ModelAttribute PageQueryModel pageQueryModel) {
        BaseModel model = new BaseModel();
        try {
            groupService.selectByGroupName(user_id, text, pageQueryModel, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @ApiOperation(value = "查询用户已加入圈子列表")
    @RequestMapping(value = "/searchUserJoin", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel searchUserJoin(@ApiParam(value = "user_id", required = true) @RequestParam("user_id") int user_id
                                    ) {
        BaseModel model = new BaseModel();
        try {
            groupService.selectUserJoin(user_id, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @ApiOperation(value = "无条件分页查询圈子信息")
    @RequestMapping(value = "/selectAllByPage", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel selectAllByPage(@ModelAttribute PageQueryModel pageQueryModel) {
        BaseModel model = new BaseModel();
        try {
            groupService.selectAllbypage(pageQueryModel, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @ApiOperation(value = "根据用户id查询所有圈子信息（附带已加入信息)")
    @RequestMapping(value = "/selectAllByUserId", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel selectAllByPage(@ApiParam(value = "user_id", required = true) @RequestParam("user_id") int user_id,
                                     @ModelAttribute PageQueryModel pageQueryModel) {
        BaseModel model = new BaseModel();
        try {
            groupService.selectAllbypage(user_id, pageQueryModel, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @ApiOperation(value = "查询单个圈子信息")
    @RequestMapping(value = "/selectById", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel selectById(@ApiParam(value = "圈子id", required = true) @RequestParam("group_id") int group_id
    ) {
        BaseModel model = new BaseModel();
        try {
            groupService.selectbygroup_id(group_id, model);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return model;
    }

    @ApiOperation(value = "通过标签查询")
    @RequestMapping(value = "/selectByTag", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel selectbytag(@ApiParam(value = "圈子标签", required = true) @RequestParam("tag") String tag,
                                 @ModelAttribute PageQueryModel pageQueryModel) {
        BaseModel model = new BaseModel();
        try {
            groupService.selectbytag(tag, pageQueryModel, model);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return model;
    }

    @ApiOperation(value = "新增圈子")
    @RequestMapping(value = "/insert", method = RequestMethod.POST, consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public BaseModel insert(@ApiParam(value = "圈子name", required = true) @RequestParam("group_name") String group_name,
                            @ApiParam(value = "圈子place_id", required = false, defaultValue = "1") @RequestParam("image_name") int place_id,
                            @ApiParam(value = "圈子tag", required = false) @RequestParam("tag") String tag,
                            @ApiParam(value = "圈子介绍", required = false) @RequestParam("intro") String intro,
                            @ApiParam(value = "上传的文件", required = true) MultipartFile file, HttpServletRequest request) {
        BaseModel model = new BaseModel();
        try {
            Group group = new Group();
            group.setGroupName(group_name);
            group.setGroupPlaceId(place_id);
            group.setGroupTag1(tag);
            group.setGroupIntro(intro);
            groupService.insertnewgroup(group, file, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @ApiOperation(value = "更新圈子信息")
    @RequestMapping(value = "/update", method = {RequestMethod.GET})
    public BaseModel getimagebytag(@ApiParam(value = "圈子id", required = true) @RequestParam("group_id") int group_id,
                                   @ApiParam(value = "圈子name", required = false) @RequestParam("group_name") String group_name,
                                   @ApiParam(value = "圈子place_id", required = false, defaultValue = "1") @RequestParam("image_name") int place_id,
                                   @ApiParam(value = "圈子tag", required = false) @RequestParam("tag") String tag,
                                   @ApiParam(value = "圈子介绍", required = false) @RequestParam("intro") String intro
    ) {
        BaseModel model = new BaseModel();
        try {
            Group group = new Group();
            group.setGroupId(group_id);
            group.setGroupName(group_name);
            group.setGroupPlaceId(place_id);
            group.setGroupTag1(tag);
            group.setGroupIntro(intro);
            groupService.updategroupintro(group, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }


}
